package com.yikaiye.android.yikaiye.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.DialogAgreementBinding;
import com.yikaiye.android.yikaiye.ui.activity.DesActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.ScreenUtil;
import com.yikaiye.android.yikaiye.utils.SpanContentParser;

/**
 * 隐私政策弹窗
 */
public abstract class AgreementDialog {
    private final Dialog dialog;

    public AgreementDialog(Activity activity) {
        DialogAgreementBinding binding = DialogAgreementBinding.inflate(LayoutInflater.from(activity));
        dialog = new Dialog(activity, R.style.prompt_progress_dialog_dimenabled);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((
                LinearLayout.LayoutParams.MATCH_PARENT), LinearLayout.LayoutParams.MATCH_PARENT);
        dialog.setContentView(binding.getRoot(), layoutParams);
        layoutParams.width = ScreenUtil.getMaxWidth(activity) - ScreenUtil.dip2px(80);
        dialog.setCancelable(false);
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
        }
        binding.tvContent.setHighlightColor(0x00000000);
        SpanContentParser span = new SpanContentParser();
        span.setBookCharListener(name -> {
            if (!CommonUtil.isCanClick()) return;
            Bundle bundle = new Bundle();
            bundle.putString(Constant.NAME_KEY, activity.getString(R.string.privacy_policy2).equals(name) ? activity.getString(R.string.privacy_policy) : activity.getString(R.string.user_agreement));
            JumpIntent.jump(activity, DesActivity.class, bundle);
        });
        binding.tvContent.setText(span.parseContent(activity.getString(R.string.agree_protocol)));
        binding.tvContent.setMovementMethod(LinkMovementMethod.getInstance());

        binding.tvLeft.setOnClickListener(v -> {
            leftClick();
        });
        binding.tvRight.setOnClickListener(v -> {
            rightClick();
        });
    }

    public abstract void leftClick();

    public abstract void rightClick();

    public void showDialog() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
